package controller.commoncontroller;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import view.UtilityConstants;

/**
 * <b>Implementiert:</b> <code>ActionListener</code> <br>
 * <br>
 * Zugehoerige Controllerklasse zum Menueeintrag <i>Ueber Locsi</i>. Diese
 * Klasse erzeugt ein Fenster, in dem Informationen ueber das Programm angezeigt
 * werden.
 * 
 * @author Team Locsi
 * 
 */
public class AboutLocsiListener extends MouseAdapter implements ActionListener {

	private static JDialog dialog = null;

	public AboutLocsiListener() {
		if (dialog == null) {
			initializeAboutLocsiDialog();
		}
	}

	/**
	 * Führt das selbe aus wie die actionPerformed
	 */
	@Override
	public void mouseClicked(MouseEvent e) {
		actionPerformed(null);
	}

	/**
	 * In dieser Methode wird ein <code>JDialog</code> erzeugt, in dem
	 * Informationen ueber das Programm angezeigt werden.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		dialog.setVisible(true);
	}

	private void initializeAboutLocsiDialog() {
		dialog = new JDialog();
		dialog.setIconImage(UtilityDialogs.getFrame().getIconImage());
		dialog.setLayout(new BorderLayout(20, 20));

		// Logo
		final JLabel grpPic = new JLabel(new ImageIcon(getClass().getClassLoader().getResource(
				UtilityConstants.ICON_PATH + "teampics"
						+ "/" + "locsi-gruppenbild.png")));
		grpPic.setBorder(BorderFactory.createEmptyBorder());
		dialog.add(grpPic, BorderLayout.CENTER);
		MouseAdapter myListener = null;
		final JLabel woife = new JLabel(
				"<html><center>Wolfgang Pfeffer</center></html>");
		woife.setFont(new Font(woife.getFont().getName(), woife
				.getFont().getStyle(), 13));
		woife.setHorizontalAlignment(SwingConstants.CENTER);
		final JLabel tobi = new JLabel(
				"<html><center>Tobias Gaisbauer</center></html>");
		tobi.setFont(new Font(tobi.getFont().getName(), tobi
				.getFont().getStyle(), 13));
		tobi.setHorizontalAlignment(SwingConstants.CENTER);
		final JLabel tom = new JLabel(
				"<html><center>Thomas Dengler</center></html>");
		tom.setFont(new Font(tom.getFont().getName(), tom
				.getFont().getStyle(), 13));
		tom.setHorizontalAlignment(SwingConstants.CENTER);
		final JLabel simon = new JLabel(
				"<html><center>Simon Höfle</center></html>");
		simon.setFont(new Font(simon.getFont().getName(), simon
				.getFont().getStyle(), 13));
		simon.setHorizontalAlignment(SwingConstants.CENTER);
		final JLabel steffi = new JLabel(
				"<html><center>Stefanie Mulik</center></html>");
		steffi.setFont(new Font(steffi.getFont().getName(), steffi
				.getFont().getStyle(), 13));
		steffi.setHorizontalAlignment(SwingConstants.CENTER);
		final JLabel email = new JLabel(
				"<html><center><br>locsi.sep@googlemail.com</center></html>");
		email.setFont(new Font(email.getFont().getName(), email
				.getFont().getStyle(), 13));
		email.setHorizontalAlignment(SwingConstants.CENTER);
		email.setToolTipText("Bei Fragen oder Feedback schreib uns einfach eine Mail!");
		myListener = new MouseAdapter() {

			@Override
			public void mouseEntered(MouseEvent e) {
				((JLabel) e.getSource()).setForeground(Color.RED);
				((JLabel) e.getSource()).setFont(new Font(((JLabel) e
						.getSource()).getFont().getName(), Font.ITALIC
						| Font.BOLD, 13));
				if (e.getSource().equals(woife)) {
					grpPic.setIcon(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
							+ "teampics" + "/"
							+ "woife.png")));
				} else if (e.getSource().equals(tobi)) {
					grpPic.setIcon(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
							+ "teampics" + "/"
							+ "tobi.png")));
				} else if (e.getSource().equals(tom)) {
					grpPic.setIcon(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
							+ "teampics" + "/"
							+ "tom.png")));
				} else if (e.getSource().equals(simon)) {
					grpPic.setIcon(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
							+ "teampics" + "/"
							+ "simon.png")));
				} else if (e.getSource().equals(steffi)) {
					grpPic.setIcon(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
							+ "teampics" + "/"
							+ "steffi.png")));
				} else if (e.getSource().equals(email)) {
					email.setCursor(Cursor
							.getPredefinedCursor(Cursor.HAND_CURSOR));
					grpPic.setIcon(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
							+ "teampics" + "/"
							+ "locsi-gruppenbild.png")));
				}
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getSource().equals(email)) {
					try {
						Desktop.getDesktop().mail(
								new URI("mailto:locsi.sep@googlemail.com"));
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}

			@Override
			public void mouseExited(MouseEvent e) {
				email.setCursor(Cursor.getDefaultCursor());
				((JLabel) e.getSource()).setForeground(Color.DARK_GRAY);
				((JLabel) e.getSource()).setFont(new Font(((JLabel) e
						.getSource()).getFont().getName(), Font.BOLD, 13));
				grpPic.setIcon(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
						+ "teampics" + "/"
						+ "locsi-gruppenbild.png")));

			}
		};
		woife.addMouseListener(myListener);
		tobi.addMouseListener(myListener);
		tom.addMouseListener(myListener);
		simon.addMouseListener(myListener);
		steffi.addMouseListener(myListener);
		email.addMouseListener(myListener);
		Box teamNameBox = Box.createVerticalBox();
		teamNameBox.add(woife);
		teamNameBox.add(tobi);
		teamNameBox.add(tom);
		teamNameBox.add(simon);
		teamNameBox.add(steffi);
		teamNameBox.add(email);
		teamNameBox.add(Box.createVerticalStrut(25));
		teamNameBox.add(new JLabel("<html><i>\u00a9 2011 - Locsi 1.0</i></html>"));
		dialog.add(teamNameBox, BorderLayout.SOUTH);

		// Ueberschrift
		JLabel headline = new JLabel(
				"<html><body><center><br><i>Team Locsi</i></center></body></html>");
		headline.setFont(new Font(headline.getFont().getName(), headline
				.getFont().getStyle(), 20));
		headline.setHorizontalAlignment(SwingConstants.CENTER);
		dialog.add(headline, BorderLayout.NORTH);
		dialog.pack();
		dialog.setResizable(false);
		dialog.setVisible(false);
		dialog.setTitle("Über Locsi...");
		dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		dialog.setLocationRelativeTo(UtilityDialogs.getFrame());
	}
}